<?php
namespace api\modules\h5\controllers\store;

use backend\widgets\provinces\Provinces;
use Yii;
use yii\web\NotFoundHttpException;
use api\controllers\OnAuthController;
use common\enums\StatusEnum;
use common\helpers\ResultDataHelper;


/**
 * 店铺接口
 *
 * Class InfoController
 * @package api\modules\h5\controllers\store
 * @property \yii\db\ActiveRecord $modelClass
 * @author tjj
 */
class InfoController extends OnAuthController
{
    public $modelClass = 'common\models\store\Store';

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = ['view'];

    /**
     * 获取店铺信息
     * @param $id
     * @return \yii\db\ActiveRecord
     * @throws NotFoundHttpException
     */
    public function actionView($id)
    {
        $model = $this->modelClass::find()
            ->select(['id','name','logo','picture','business_time','telephone','address_name','address_detail','status'])
            ->where(['id'=>$id])
            ->asArray()
            ->one();

        if(!$model)
            throw new NotFoundHttpException('请求的数据不存在或您的权限不足.');

        if(0 == $model['status'])
            throw new NotFoundHttpException('店铺已关闭.');

        return $model;
    }

    /**
     * 权限验证
     *
     * @param string $action 当前的方法
     * @param null $model 当前的模型类
     * @param array $params $_GET变量
     * @throws \yii\web\BadRequestHttpException
     */
    public function checkAccess($action, $model = null, $params = [])
    {
        // 方法名称
        if (in_array($action, ['delete', 'index','update','create']))
        {
            throw new \yii\web\BadRequestHttpException('权限不足');
        }
    }
}
